package com.tr.rentcar.common.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class PageTag extends SimpleTagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int pageSize=5;//每页显示记录数
	private int pageNo=1;//当前页号
	private int totalCount;//总记录数
	
	@Override
	public void doTag() throws JspException, IOException {
		
		int pageCount=(getTotalCount()+getPageSize()-1)/getPageSize();//总页数
		JspWriter jw=this.getJspContext().getOut();
		/*HttpServletRequest request=(HttpServletRequest) ((PageContext)this.getJspContext()).getRequest();
		Long count=(Long) request.getAttribute("totalCount");
		System.out.println("测试"+count);*/
		
		
		//System.out.println("总记录数"+getTotalCount());
		//System.out.println("总页数"+pageCount);
		
		//拼写HTML
		StringBuffer sb=new StringBuffer();
		sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"statics/css/pageTag.css\" />\r\n");
		sb.append("<script type=\"text/javascript\" src=\"statics/js/pageTag.js\" charset=\"UTF-8\"></script>\r\n");
		sb.append("<div class=\"page\">");
		sb.append("<span class=\"lastPage\">上一页</span>\r\n");
		for (int i = 0; i < pageCount; i++) {
			if(i==0){
				sb.append("<a data-page=\"").append(i+1).append("\" class=\"pageSelect\">").append(i+1).append("</a>");
			}else{
				sb.append("<a data-page=\"").append(i+1).append("\">").append(i+1).append("</a>");
			}
		}
		sb.append("<span class=\"nextPage\">下一页</span>\r\n");
		sb.append("<input type=\"hidden\" name=\"pageNo\" value=\""+getPageNo()+"\">");
		sb.append(getPageNo()+"/"+pageCount+"\t（总记录："+getTotalCount()+"条）");
		sb.append("</div>");
		
		jw.write(sb.toString());
	
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	
	

}
